<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：用户收益控制器。
 *
 **/
namespace Api\Controller;

class IncomeController extends ComController
{
    /**
     * 收益数据汇总
     */
    public function index()
    {
        $s = strtotime(date('Y-m-d').'00:00:00');
        $l = strtotime(date('Y-m-d').'23:59:59');
        
        //今日收益
        $today_income = M('user_income')->where(array('uid'=>$this->user['id'],'t'=>array('between',array($s,$l))))->sum('money');
        if(!$today_income){
            $today_income = 0;
        }
        $data['today_income'] = $today_income;
        
        //收益总额
        $total_income = M('user_income')->where(array('uid'=>$this->user['id']))->sum('money');
        if(!$total_income){
            $total_income = 0;
        }
        $data['total_income'] = $total_income;
        $this->res['data'] = $data;
        $this->response($this->res,'json');
    }
    
    /**
     * 任务收益
     */
    public function taskIncome()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $m = M('user_income');
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $where['uid'] = $this->user['id'];
        $where['type'] = array('in','1,2,3,4');
        $count = $m->where($where)->count();
        $list = $m->order('t desc')->where($where)->field('id,money,t,type,remark')->limit($offset . ',' . $pagesize)->select();
        
        foreach ($list as $key=>&$val){
            $list[$key]['t'] = date('Y-m-d H:i:s',$val['t']);
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
    
    /**
     * 团队收益
     */
    public function teamIncome()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $m = M('user_income');
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $m
                   ->alias("a")
                   ->join('LEFT JOIN '.C('DB_PREFIX').'user b on a.uid=b.id')
                   ->where(array('b.pid'=>$this->user['id']))
                   ->count(); 
        $list = $m
                   ->alias("a")
                   ->join('LEFT JOIN '.C('DB_PREFIX').'user b on a.uid=b.id')
                   ->field('a.id,a.remark,a.t,a.type,a.money,b.username as mobile')
                   ->where(array('b.pid'=>$this->user['id']))
                   ->order('a.t desc')
                   ->limit($offset . ',' . $pagesize)
                   ->select();
        foreach ($list as $key=>&$val){
            $list[$key]['t'] = date('Y-m-d H:i:s',$val['t']);
            $list[$key]['mobile'] = yc_phone($val['mobile']);
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
}